package binary_tree.Leetcode;

/**
 * 传入以root为根节点的二叉树，能将其按照谦虚遍历的方式转为字符串
 */
public class Num606_Tree2String {
    private StringBuilder sb=new StringBuilder();
    public String tree2str(TreeNode root) {
        if (root==null){
            return " ";
        }
        //先访问根节点
        sb.append(root.val);
        //在访问左子树
        if(root.left!=null){
            sb.append("(");
            tree2str(root.left);
            sb.append(")");
        }else {
            if(root.right!=null){
                sb.append("()");
            }
        }
        if(root.right!=null){
            sb.append("(");
            tree2str(root.right);
            sb.append(")");
        }
        return sb.toString();
    }
}
